package ynu.edu.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import ynu.edu.dto.PaymentDTO;
import ynu.edu.dto.PaymentRequest;
import ynu.edu.entities.BaseResponse;
import ynu.edu.service.PaymentService;

import java.util.List;

/**
 * 支付控制器
 */
@Slf4j
@RestController
@RequestMapping("/payment")
public class PaymentController {
    
    private final PaymentService paymentService;
    
    public PaymentController(PaymentService paymentService) {
        this.paymentService = paymentService;
    }
    
    /**
     * 创建支付订单
     *
     * @param request 支付请求
     * @return 支付信息
     */
    @PostMapping("/create")
    public BaseResponse<PaymentDTO> createPayment(@RequestBody PaymentRequest request) {
        log.info("收到创建支付订单请求：{}", request);
        PaymentDTO payment = paymentService.createPayment(request);
        return BaseResponse.success(payment);
    }
    
    /**
     * 处理支付
     *
     * @param paymentId 支付ID
     * @param transactionId 交易号
     * @return 支付信息
     */
    @PutMapping("/process/{paymentId}")
    public BaseResponse<PaymentDTO> processPayment(
            @PathVariable("paymentId") Integer paymentId,
            @RequestParam String transactionId) {
        log.info("收到处理支付请求，支付ID：{}，交易号：{}", paymentId, transactionId);
        PaymentDTO payment = paymentService.processPayment(paymentId, transactionId);
        return BaseResponse.success(payment);
    }
    
    /**
     * 取消支付
     *
     * @param paymentId 支付ID
     * @return 支付信息
     */
    @PutMapping("/cancel/{paymentId}")
    public BaseResponse<PaymentDTO> cancelPayment(@PathVariable("paymentId") Integer paymentId) {
        log.info("收到取消支付请求，支付ID：{}", paymentId);
        PaymentDTO payment = paymentService.cancelPayment(paymentId);
        return BaseResponse.success(payment);
    }
    
    /**
     * 支付成功
     *
     * @param paymentId 支付ID
     * @return 支付信息
     */
    @PutMapping("/paySuccess/{paymentId}")
    public BaseResponse<PaymentDTO> paySuccess(@PathVariable("paymentId") Integer paymentId) {
        log.info("收到支付成功请求，支付ID：{}", paymentId);
        PaymentDTO payment = paymentService.paySuccess(paymentId);
        return BaseResponse.success(payment);
    }
    
    /**
     * 支付成功 - POST方法（兼容前端API调用）
     *
     * @param paymentId 支付ID
     * @return 支付信息
     */
    @PostMapping("/paySuccess/{paymentId}")
    public BaseResponse<PaymentDTO> paySuccessPost(@PathVariable("paymentId") Integer paymentId) {
        log.info("收到POST方法支付成功请求，支付ID：{}", paymentId);
        PaymentDTO payment = paymentService.paySuccess(paymentId);
        return BaseResponse.success(payment);
    }
    
    /**
     * 查询支付信息
     *
     * @param paymentId 支付ID
     * @return 支付信息
     */
    @GetMapping("/{paymentId}")
    public BaseResponse<PaymentDTO> getPayment(@PathVariable("paymentId") Integer paymentId) {
        log.info("收到查询支付信息请求，支付ID：{}", paymentId);
        PaymentDTO payment = paymentService.getPayment(paymentId);
        return BaseResponse.success(payment);
    }
    
    /**
     * 根据订单ID查询支付信息
     *
     * @param orderId 订单ID
     * @return 支付信息
     */
    @GetMapping("/order/{orderId}")
    public BaseResponse<PaymentDTO> getPaymentByOrderId(@PathVariable("orderId") Integer orderId) {
        log.info("收到根据订单ID查询支付信息请求，订单ID：{}", orderId);
        PaymentDTO payment = paymentService.getPaymentByOrderId(orderId);
        return BaseResponse.success(payment);
    }
    
    /**
     * 查询用户支付记录
     *
     * @param userId 用户ID
     * @return 支付记录列表
     */
    @GetMapping("/user/{userId}")
    public BaseResponse<List<PaymentDTO>> getUserPayments(@PathVariable("userId") String userId) {
        log.info("收到查询用户支付记录请求，用户ID：{}", userId);
        List<PaymentDTO> payments = paymentService.getUserPayments(userId);
        return BaseResponse.success(payments);
    }
    
    /**
     * 根据支付状态查询用户支付记录
     *
     * @param userId 用户ID
     * @param status 支付状态
     * @return 支付记录列表
     */
    @GetMapping("/user/{userId}/status/{status}")
    public BaseResponse<List<PaymentDTO>> getUserPaymentsByStatus(
            @PathVariable("userId") String userId,
            @PathVariable("status") Integer status) {
        log.info("收到根据支付状态查询用户支付记录请求，用户ID：{}，状态：{}", userId, status);
        List<PaymentDTO> payments = paymentService.getUserPaymentsByStatus(userId, status);
        return BaseResponse.success(payments);
    }
} 